package com.sh.data.engine.domain.integration.documentCapture.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xssf.model.SharedStrings;
import org.apache.poi.xssf.model.StylesTable;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

public class XSSFHandler extends DefaultHandler {

    private static final Integer STRING_TYPE = 1;

    private static final Integer DOUBLE_TYPE = 2;

    private SharedStrings sharedStringsTable;
    private StylesTable stylesTable;
    private List<List<String>> data;
    private List<String> currentRow;
    private StringBuilder currentCellValue;
    private int currentCellType;

    // 构造函数
    public XSSFHandler(SharedStrings sharedStringsTable, StylesTable stylesTable) {
        this.sharedStringsTable = sharedStringsTable;
        this.stylesTable = stylesTable;
        this.data = new ArrayList<>();
        this.currentRow = new ArrayList<>();
        this.currentCellValue = new StringBuilder();
    }

    // 返回解析结果
    public List<List<String>> getData() {
        return data;
    }

    // 处理开始标签
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("row".equals(qName)) {
            // 每一行开始时，创建一个新的列表
            currentRow = new ArrayList<>();
        } else if ("c".equals(qName)) { // "c" 是单元格的标签
            String cellType = attributes.getValue("t"); // 获取单元格类型
            if ("s".equals(cellType)) {
                currentCellType = STRING_TYPE; // 字符串类型
            } else if ("str".equals(cellType)) {
                currentCellType = STRING_TYPE; // 强制认为是字符串
            } else {
                currentCellType = DOUBLE_TYPE; // 默认处理为数字类型
            }
        }
    }

    // 处理单元格内容
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length).trim();
        if (!value.isEmpty()) {
            // 累积单元格的值
            currentCellValue.append(value);
        }
    }

    // 处理结束标签
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("c".equals(qName)) { // 结束单元格标签时，处理单元格的值
            if (currentCellType == DOUBLE_TYPE) {
                try {
                    double numericValue = Double.parseDouble(currentCellValue.toString());
                    currentRow.add(String.valueOf(numericValue)); // 将数字作为字符串存储
                } catch (NumberFormatException e) {
                    currentRow.add(currentCellValue.toString()); // 如果无法转换为数字，则保留原始值
                }
            } else {
                // 如果是字符串类型，使用 SharedStringsTable 转换为实际字符串
                if (currentCellType == STRING_TYPE) {
                    String sharedString = currentCellValue.toString();
                    int idx = Integer.parseInt(sharedString);
                    currentRow.add(sharedStringsTable.getItemAt(idx).toString());
                }
            }
            // 清空单元格值
            currentCellValue.setLength(0);
        } else if ("row".equals(qName)) {
            if (CollectionUtils.isNotEmpty(currentRow)) {
                data.add(new ArrayList<>(currentRow));
            }
            // 每一行结束时，将当前行的数据添加到 data 中
        }
    }
}
